package com.it.system.course_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.system.course_service.config.CourseMQConfig;
import com.it.system.course_service.service.CourseService;
import com.it.system.educommonapi.entity.Course;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;


    /**
     * 旧的方法保留以兼容现有代码
     * @deprecated 使用 getCourseList 代替
     */
    @GetMapping("/courses/all")
    public List<Course> getAllCourse() {
        return courseService.getAllCourse();
    }

    @GetMapping("/course/{id}")
    public Course getCourseById(@PathVariable long id) {
        return courseService.getCourseById(id);
    }
    
    /**
     * 分页获取课程列表，支持筛选和排序
     * @param current 当前页
     * @param size 每页大小
     * @param typeId 课程类型ID
     * @param orientationId 课程方向ID
     * @param subOrientationId 课程子方向ID
     * @param keywords 关键词搜索
     * @param orderField 排序字段
     * @param orderType 排序类型
     * @return 分页课程列表
     */
    @GetMapping("/courses")
    public Page<Course> getCourseList(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) Long typeId,
            @RequestParam(required = false) Long orientationId,
            @RequestParam(required = false) Long subOrientationId,
            @RequestParam(required = false) String keywords,
            @RequestParam(defaultValue = "sales") String orderField,
            @RequestParam(defaultValue = "desc") String orderType) {
            
        return courseService.getCourseListPage(current, size, typeId, orientationId, 
                subOrientationId, keywords, orderField, orderType);
    }

    @Resource
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/sendMessage")
    public String sendMessage(String msg,String key){
        rabbitTemplate.convertAndSend(CourseMQConfig.EXCHANGE,key,msg);
        return "success";
    }

}
